package com.xueyu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xueyu.domain.sys.Sysuser;
import com.xueyu.service.SysuserService;
import com.xueyu.util.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sysuser")
public class SysuserController {

    @Autowired
    private final SysuserService sysuserService;

    @Autowired
    public SysuserController(SysuserService sysuserService) {
        this.sysuserService = sysuserService;
    }

    // 添加用户
    @PostMapping("/add")
    public ResultBean addUser(@RequestBody Sysuser sysuser) {
        boolean success = sysuserService.save(sysuser);
        return success ? new ResultBean() : new ResultBean(500, "添加用户失败");
    }

    @DeleteMapping("/delete/{id}")
    public ResultBean deleteUser(@PathVariable("id") Long id) {
        boolean success = sysuserService.removeById(id);
        return success ? new ResultBean() : new ResultBean(500, "Failed to delete user");
    }

    @PutMapping("/update")
    public ResultBean updateUser(@RequestBody Sysuser sysuser) {
        boolean success = sysuserService.updateById(sysuser);
        return success ? new ResultBean() : new ResultBean(500, "Failed to update user");
    }

    @GetMapping("/{id}")
    public ResultBean getUserById(@PathVariable("id") Long id) {
        Sysuser user = sysuserService.getById(id);
        return user != null ? new ResultBean(){{ put("data", user); }} : new ResultBean(404, "User not found");
    }

    @PostMapping("/list")
    public ResultBean getAllUsers(@RequestBody Map<String, Object> params) {
        Integer pageNo = Integer.parseInt(params.get("Current").toString());
        Integer pageSize = Integer.parseInt(params.get("size").toString());

        IPage<Sysuser> page = new Page<>(pageNo, pageSize);
        IPage<Sysuser> userPage = sysuserService.page(page, null);
        return new ResultBean(){{ put("data", userPage.getRecords()); put("total", userPage.getTotal()); }};
    }



    @GetMapping("/search")
    public ResultBean searchUsers(@RequestParam("keyword") String keyword) {
        List<Sysuser> users = sysuserService.searchUsers(keyword);
        if (users != null) {
            ResultBean result = new ResultBean();
            result.put("data", users);
            return result;
        } else {
            return new ResultBean(404, "未找到用户");
        }
    }

    //查询角色
    @GetMapping("/roles")
    public ResultBean getAllRoles() {
        List<Integer> roles = sysuserService.getAllRoles();
        return roles != null ? new ResultBean(){{ put("data", roles); }} : new ResultBean(404, "No roles found");
    }

    //查询状态
    @GetMapping("/statuses")
    public ResultBean getAllStatuses() {
        List<Integer> statuses = sysuserService.getAllStatuses();
        return statuses != null ? new ResultBean(){{ put("data", statuses); }} : new ResultBean(404, "No statuses found");
    }


}
